﻿Public Class Login
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    End Sub

    Private Sub LimpaForms()

        lblErro.Visible = False
        lblErroRegister.Visible = False

        lblErro.Text = String.Empty
        lblErroRegister.Text = String.Empty
        txtUser.Text = String.Empty
        txtUserName.Text = String.Empty
        txtDisplayName.Text = String.Empty
        txtEmail.Text = String.Empty
        txtPass.Text = String.Empty
        txtPassword.Text = String.Empty
        txtConfirmarPass.Text = String.Empty

    End Sub

    Protected Sub lnkBtnLogin_Click(ByVal sender As Object, ByVal e As EventArgs) Handles lnkBtnLogin.Click

        pnlLogin.Enabled = False

        Dim BLApostas As New BusinessLogic.BLApostas.BLApostas

        Dim DisplayName As String = String.Empty
        Dim IDUtilizador As Integer = Nothing

        Dim Resultado As String = BLApostas.AutenticarUtilizador(txtUserName.Text, txtPassword.Text, DisplayName, IDUtilizador)

        If Resultado.Equals("OK") Then

            System.Threading.Thread.Sleep(1000)

            'strURL = "~/Home.aspx?idUser=" & TabId & "&processoid=" & IDProcesso
            'Response.Redirect(strURL, True)

            Session("IDUtilizador") = IDUtilizador

            FormsAuthentication.RedirectFromLoginPage(DisplayName, False)

        Else

            'ScriptManager.RegisterStartupScript(Me, GetType(Page), "mensagem", "alert('" & Resultado.ToString() & "');", True)

            lblErro.Text = Resultado.ToString()
            lblErro.Visible = True

        End If

        pnlLogin.Enabled = True

    End Sub

    Protected Sub lnbCriarUser_Click(ByVal sender As Object, ByVal e As EventArgs) Handles lnbCriarUser.Click

        Dim BLApostas As New BusinessLogic.BLApostas.BLApostas

        Dim Resultado As String = BLApostas.ValidarRegistar(txtUser.Text, txtDisplayName.Text, txtEmail.Text, txtPass.Text, txtConfirmarPass.Text)

        If Resultado.Equals("OK") Then

            lblErroRegister.Text = "Utilizador Criado com Sucesso!"
            lblErroRegister.Visible = True
            LimpaForms()

        Else

            lblErroRegister.Text = Resultado.ToString()
            lblErroRegister.Visible = True

        End If

        ModalPopupExtender1.Show()

    End Sub

    Protected Sub lnkVoltar_Click(ByVal sender As Object, ByVal e As EventArgs) Handles lnkVoltar.Click
        LimpaForms()
    End Sub

    Protected Sub lnkRegister_Click(ByVal sender As Object, ByVal e As EventArgs) Handles lnkRegister.Click
        LimpaForms()
    End Sub

End Class